﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MySql.Data.Types;



namespace Gui
{
    public partial class Interface : Form
    {
        private int projectnummer;
        MySqlConnection grabConnection = new MySqlConnection("Server=qq-design.com;Port=3306;Database=cookicom0;Uid=cookicom0;pwd=pizzahut91");
        private int userId;
        private int messageId;
        private int[] messageIDs = new int[100];
        private int[] id;
        private string[] title;
        private string[] description;
        int projectAmount;
        private int[] receiverIDs = new int[100];
        public Interface()
        {
               Gui.Functies.Login loadingDone = new Gui.Functies.Login();
        }

        public Interface(int project, int userID, int[] ids, string[] titles, string[] descriptions, int projectAantal)
        {
            projectnummer = project;
            id = ids;
            title = titles;
            description = descriptions;
            projectAmount = projectAantal;
            userId = userID;
            InitializeComponent();
            textBox1.Hide();
            panel1.Hide();
            panel2.Hide();
            button2.Hide();
            huidigeTaken(grabConnection);
            agendaToday(grabConnection);
            label2_motd(grabConnection);
        
            collectPM(grabConnection);
        }

        private void buttonHome_Click(object sender, EventArgs e)
        {
            MessageBox.Show(projectnummer.ToString());
        }

        private void btnChat_Click(object sender, EventArgs e)
        {
            Chat.Chat chat = new Chat.Chat();
            chat.Show();
        }

        private void btnAgenda_Click(object sender, EventArgs e)
        {     
            loadFunction(1);
        }

        private void btnTakenlijst_Click(object sender, EventArgs e)
        {
            loadFunction(2);
        }

        private void btnFileshare_Click(object sender, EventArgs e)
        {
            loadFunction(3);
        }

        private void loadFunction(int choice)
        {
            int panel = choice;

            switch(panel)
            {
                case 1:                    
                    //zie functies -> agenda + agendapunten
                    panelMain.Controls.Clear();
                    panelMain.Controls.Add(new Functies.Agenda(projectnummer));
                    break;
                case 2:
                    //zie functies -> takenlijst
                    panelMain.Controls.Clear();
                    panelMain.Controls.Add(new Functies.Takenlijst(projectnummer));
                    break;
                case 3:
                    //zie functies -> fileshare
                    panelMain.Controls.Clear();
                    panelMain.Controls.Add(new Functies.Fileshare(projectnummer));
                    break;              
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        // Haalt de message of the day op.
        private void label2_motd(MySqlConnection grabConnection)
        {
            try
            {
                MySqlCommand motdGet = new MySqlCommand("SELECT motd FROM motd WHERE projectID=@projectID", grabConnection);
                motdGet.Parameters.Add(new MySqlParameter("projectID", projectnummer));
                grabConnection.Open();
                MySqlDataReader reader = motdGet.ExecuteReader();

                while (reader.Read())
                {
                    label2.Text = reader.GetString(0);
                }
                    reader.Dispose();
                    motdGet.Dispose();
                
                
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
            }
            grabConnection.Close();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Hide();
            button2.Hide();
            editMotd(grabConnection);
            label1.Show();
            label2.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            label2.Hide();
            textBox1.Show();
            button2.Show();

        }
        //Methode om de message of the day aan te passen.
        private void editMotd(MySqlConnection grabConnection)
        {
            String newMotd = textBox1.Text;
           
            if (newMotd != "")
            {
                MySqlCommand editMotd = new MySqlCommand("UPDATE motd SET motd=@motd WHERE projectID=@projectID", grabConnection);
                editMotd.Parameters.Add(new MySqlParameter("motd", newMotd));
                editMotd.Parameters.Add(new MySqlParameter("projectID", projectnummer));

                try
                {
                    grabConnection.Open();
                    editMotd.ExecuteNonQuery();
                    

                }
                catch (MySqlException)
                {
                   
                    MessageBox.Show("sql error detected!");
                }
               
                grabConnection.Close();
                editMotd.Dispose();
                label2_motd(grabConnection);
            }
            else
            {
                MessageBox.Show("Geen nieuwe Message of the day ingesteld");
            }

           
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }
        //haal de meest recent agenda op.
        private void agendaToday(MySqlConnection grabConnection)
        {
            try
            {
                MySqlCommand dayagendaGet = new MySqlCommand("SELECT note FROM agenda WHERE projectID=@projectID", grabConnection);
                dayagendaGet.Parameters.Add(new MySqlParameter("projectID", projectnummer));
                grabConnection.Open();
                MySqlDataReader reader = dayagendaGet.ExecuteReader();
                while (reader.Read())
                {
                    textBox2.Text = reader.GetString(0);

                    if (reader.GetString(0) == "")
                    {
                        textBox2.Text = "No current agenda.";
                    }
                }   
                 
                    reader.Dispose();
                    dayagendaGet.Dispose();
                
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
                textBox2.Text = "No current agenda.";
            }

            grabConnection.Close();
        }
        //Haalt de private message inhoud op.
        public void collectPM(MySqlConnection GrabConnection)
        {
            try
            {
                MySqlCommand privateMessages = new MySqlCommand("SELECT messageID, subject FROM private_message WHERE userID=@userID", grabConnection);
                privateMessages.Parameters.Add(new MySqlParameter("userID", userId));
                grabConnection.Open();
                MySqlDataReader reader = privateMessages.ExecuteReader();

                if (reader.Read() != false)
                {
                    if (reader.IsDBNull(0) == true)
                    {
                        grabConnection.Close();
                        reader.Dispose();
                        privateMessages.Dispose();
                    }
                    else
                    {
                        String mId;
                        int counter = 0;
                        
                        while (reader.Read())
                        {
                            mId = reader.GetString(0);
                            messageIDs[counter] = Int32.Parse(mId);
                            checkedListBox1.Items.Add(reader.GetString(1));
                            counter++;
                        }
                    }
                    grabConnection.Close();
                    reader.Dispose();
                    privateMessages.Dispose();
                }
                grabConnection.Close();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
                checkedListBox1.Items.Add("No messages");
            }
        }

        //Haalt je private messages op voor je mailbox.
        public void getMessage(MySqlConnection grabConnection, int index)
        {
            try
            {

                messageId = messageIDs[index];
                MySqlCommand privateMessage = new MySqlCommand("SELECT senderID, subject, message FROM private_message WHERE messageID=@messageID", grabConnection);
                privateMessage.Parameters.Add(new MySqlParameter("messageID", messageId));
                grabConnection.Open();
                MySqlDataReader reader = privateMessage.ExecuteReader();
                panel1.Show();
                while (reader.Read())
                {
                    senderLabel.Text = reader.GetString(0);
                    receiveBox.Text = reader.GetString(1);
                    receiveSubject.Text = reader.GetString(2);
                }
                grabConnection.Close();
                reader.Dispose();
                privateMessage.Dispose();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());

            }
        }
        //Haalt de taken op waar mensen op het moment mee bezig zijn.
        public void huidigeTaken(MySqlConnection grabConnection)
        {
            try
            {
                String status = "Bezig";
                MySqlCommand currentTasks = new MySqlCommand("SELECT Taak FROM taken WHERE projectID=@projectID AND Status=@status", grabConnection);
                currentTasks.Parameters.Add(new MySqlParameter("projectID", projectnummer));
                currentTasks.Parameters.Add(new MySqlParameter("status", status));
                grabConnection.Open();
                MySqlDataReader reader = currentTasks.ExecuteReader();

            
                        while (reader.Read())
                        {
                            listBox1.Items.Add(reader.GetString(0)); 
                        }
                    grabConnection.Close();
                    reader.Dispose();
                    currentTasks.Dispose();
                
                grabConnection.Close();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
                textBox2.Text = "No current agenda.";
            }
        }
        //Looks for the users in your project to send messages to.
        public void getReceivers(MySqlConnection grabConnection)
        {
           
                int counter = 0;
                try
                {  //userIDs ophalen
                    MySqlCommand getReceiverIDs = new MySqlCommand("SELECT UserID FROM project_members WHERE ProjectID=@projectID", grabConnection);
                    getReceiverIDs.Parameters.Add(new MySqlParameter("projectID", projectnummer));
                    grabConnection.Open();
                    MySqlDataReader reader = getReceiverIDs.ExecuteReader();

                
                            String rId;
                            while (reader.Read())
                            {
                                rId = reader.GetString(0);
                                receiverIDs[counter] = Int32.Parse(rId);
                                counter++;
                            }
                       
                        reader.Dispose();
                    
                }
                catch (MySqlException e)
                {
                    MessageBox.Show(e.ToString());
                    checkedListBox1.Items.Add("No messages");
                }
              
                //Nu de ids omzetten naar namen.
                try
                {
                MySqlCommand getReceivers = new MySqlCommand("SELECT Naam, Tussenvoegsel, Achternaam FROM confirmed_users WHERE UserID=@receiverid", grabConnection);
                int counter2 = 0;
                for (int i = 0; i <= 10; i++)
                {
                    getReceivers.Parameters.Add(new MySqlParameter("receiverid", receiverIDs[counter2]));
                   MySqlDataReader reader2 = getReceivers.ExecuteReader();
                   while (reader2.Read())
                   {

                       comboBox1.Items.Add(reader2.GetString(0) + " " + reader2.GetString(1) + " " + reader2.GetString(2));
                   }



                    counter2++;
                    getReceivers.Parameters.Remove(new MySqlParameter("receiverid", receiverIDs[counter2]));
                    reader2.Dispose();
                }  
             
     
                getReceivers.Dispose();
            }
            catch (MySqlException e)
            {
                MessageBox.Show(e.ToString());
                checkedListBox1.Items.Add("No messages");
            }
                grabConnection.Close();
        }

        //Sends the private message
        public void sendMessage(MySqlConnection grabConnection)
        {
            int receiverIndex = comboBox1.SelectedIndex;
            int mUid = receiverIDs[receiverIndex];
            try
            {   
                MySqlCommand privateMessage = new MySqlCommand("INSERT INTO private_message (userID, senderID, subject, message)" + "VALUES (@mUID, @mSID, @subject, @message)", grabConnection);
                privateMessage.Parameters.Add(new MySqlParameter("mUID", mUid));
                privateMessage.Parameters.Add(new MySqlParameter("mSID", userId));
                privateMessage.Parameters.Add(new MySqlParameter("subject", textBox3.Text));
                privateMessage.Parameters.Add(new MySqlParameter("message", richTextBox1.Text));
                textBox3.Clear();
                richTextBox1.Clear();
                comboBox1.Items.Clear();
                grabConnection.Open();
                privateMessage.ExecuteNonQuery();
                grabConnection.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());

            }

        }
        //Messages worden hiermee verwijderd.
        public void deleteMessage()
        {
            try
            {
                int selectedIndex = checkedListBox1.SelectedIndex;
                int dID = messageIDs[selectedIndex];
                MySqlCommand deleteMsg = new MySqlCommand("DELETE FROM private_message WHERE messageID=@msgdelete", grabConnection); 
                deleteMsg.Parameters.Add(new MySqlParameter("msgdelete", dID));
                grabConnection.Open();
                deleteMsg.ExecuteNonQuery();
                grabConnection.Close();
                checkedListBox1.Items.Clear();
                collectPM(grabConnection);

            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }


        }
       
        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = checkedListBox1.SelectedIndex;
            getMessage(grabConnection, index);
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            panel1.Hide();
        }



        private void button4_Click(object sender, EventArgs e)
        {
           getReceivers(grabConnection);
            panel2.Show();

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            panel2.Hide();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void sendButton_Click(object sender, EventArgs e)
        {
            sendMessage(grabConnection);
            panel2.Hide();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            deleteMessage();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void logoutStrip_Click(object sender, EventArgs e)
        {

            Application.Restart();
        }

        private void exitStrip_Click(object sender, EventArgs e)
        {
            Application.Exit(); 
        }

        private void overviewStrip_Click(object sender, EventArgs e)
        {
            Gui.Functies.Project_keuzemenu.Projectkeuzemenu overview = new Functies.Project_keuzemenu.Projectkeuzemenu(id, title, description, projectAmount);
            this.Hide();
            overview.Show();
          
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }
        

    }
}